//
//  JwHttpBaser.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-30.
//

#import "JwHttpBaser.h"
#import "JwHttpManager.h"
//#import "JwLocalCenter+Model.h"

@implementation JwHttpBaser

/// 基本Url
+ (NSString *)setupBaseUrlWithPoint:(NSString *)point {
    NSString *bUrl = [JwHttpManager shared].baseUrl ?: @"";
    NSString *bPoint = [JwHttpManager shared].basePoint ?: @"";
    NSString *cPoint = point ?: @"";
    NSString *baseUrl = [NSString stringWithFormat:@"%@%@%@", bUrl, bPoint, cPoint];
    return baseUrl;
}

/// 基本参数
+ (NSMutableDictionary *)setupBaseParamsWithParam:(NSDictionary *)param {
    NSMutableDictionary *bParam = [NSMutableDictionary dictionaryWithDictionary:param];
    //[bParam addEntriesFromDictionary:param];
    return bParam;
}

/// 基本头参数
+ (NSMutableDictionary *)setupBaseHeadersWithParam:(NSDictionary *)param {
    NSMutableDictionary *bParam = [NSMutableDictionary dictionaryWithDictionary:param];
    bParam[@"os"] = @"iOS";
    bParam[@"osVersion"] = [JwDevice systemVersion];
    bParam[@"osBrand"] = [JwDevice phoneModel];
    bParam[@"devid"] = [JwUUID uuidByKeychain];
    bParam[@"timestamp"] = [JwCommon timestamp];
    
    /**
     NSString *accessToken = [JwLocalCenter shared].loginModel.token.accessToken;
     if (accessToken) {
     bParam[@"accessToken"] = accessToken;
     }     
     */
    //[bParam addEntriesFromDictionary:param];
    return bParam;
}

/// 基本库参数
+ (NSMutableDictionary *)setupBaseOwnersWithParam:(NSDictionary *)param {
    NSMutableDictionary *bParam = [NSMutableDictionary dictionary];
    bParam[kJwHttpOwnerDebugLogKey] = @"1";
    bParam[kJwHttpOwnerErrorHudKey] = @"1";
    [bParam addEntriesFromDictionary:param];
    return bParam;
}

/// 加密签名参数
+ (NSMutableDictionary *)setupSignWithParam:(NSDictionary *)param {
    
    NSMutableString *signString = [NSMutableString string];
    NSMutableDictionary *bParam = [NSMutableDictionary dictionary];
    
    NSArray *keys = [bParam allKeys];
    // 通过key获取相应的 "key=value"
    NSMutableArray *mixs = [NSMutableArray array];
    
    for (NSString *key in keys) {
        NSString *value = [bParam objectForKey:key];
        if (![JwCommon isBlank:value]) {
            NSString *mix = [NSString stringWithFormat:@"%@=%@", key, value];
            [mixs addObject:mix];
        }
    }
    // 按照字母升序排列
    NSArray *sortedMixs = [mixs sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        // 升序
        NSComparisonResult result = [obj1 compare:obj2 options:NSLiteralSearch];
        return result;
    }];
    
    [sortedMixs enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            [signString appendString:[NSString stringWithFormat:@"%@", obj]];
        } else {
            [signString appendString:[NSString stringWithFormat:@"&%@", obj]];
        }
    }];
    // 混淆参数
    // [signString appendString:[NSString stringWithFormat:@"&secret=%@", "signSecret"]];
    bParam[@"sign"] = [[signString jw_md5String] uppercaseString];
    [bParam addEntriesFromDictionary:param];
    return bParam;
}


@end
